﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_SelectProjectControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                BinderData();
            }
            Button bt_query = (Button)FindControl("bt_query");
            bt_query.Click += new EventHandler(bt_query_Click);
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.OnLoadData += new EventHandler(CustomGridView1_OnLoadData);
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Label lblHeader = (Label)FindControl("lblHeader");
            Label lblName = (Label)FindControl("lblName");
            string temp = base.Page.Server.UrlDecode(HttpContext.Current.Request["TYPE"]);
            if (string.IsNullOrEmpty(temp)) { temp = "xzxm"; }
            if (temp == "xzxm")
            {
                lblHeader.Text = "征收项目"; lblName.Text = "项目名称";
            }
            if (temp == "xzxmzj")
            {
                lblHeader.Text = "征收项目"; lblName.Text = "项目名称";
            }
            if (temp == "xzxmx")
            {
                lblHeader.Text = "征收项目"; lblName.Text = "项目名称";
            }
            if (temp == "xzxc")
            {
                lblHeader.Text = "征收项目"; lblName.Text = "项目名称";
            }
            if (temp == "khyh")
            {
                lblHeader.Text = "开户银行"; lblName.Text = "银行名称";
            }
            if (temp == "jftz")
            {
                lblHeader.Text = "征收项目"; lblName.Text = "项目名称";
            }
            if (temp == "sfxx")
            {
                lblHeader.Text = "征收项目"; lblName.Text = "项目名称";
            }
            if (temp == "xfzdry")
            {
                lblHeader.Text = "征收项目"; lblName.Text = "项目名称";
            }
            if (temp == "房屋征收补偿资金监管")
            {
                lblHeader.Text = "征收项目"; lblName.Text = "征收实施单位名称";
            }
        }
        private void BinderData()
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.RowCreated += new GridViewRowEventHandler(CustomGridView1_RowCreated);
            string sqlStr = string.Empty;
            string temp = base.Page.Server.UrlDecode(HttpContext.Current.Request["TYPE"]);
            string strQS = HttpContext.Current.Request["qs"];
            DataTable dt = _EluZSDataProvider.GetSelProject(temp, GetStrWhere(), "");
            if (!string.IsNullOrEmpty(strQS))
            {
                dt = _EluZSDataProvider.GetSelProject(temp, GetStrWhere(), strQS);
            }
            CustomGridView1.DataSource = dt;
            CustomGridView1.RecordCount = dt.Rows.Count;
            CustomGridView1.HideColName = "ID";
            CustomGridView1.DataKeyNames = new string[] { "ID" };
            CustomGridView1.DataBind();
        }
        protected string GetStrWhere()
        {
            TextBox txt_name = (TextBox)FindControl("txt_name");
            TextBox txtAddress = (TextBox)FindControl("txtAddress");
            string temp = base.Page.Server.UrlDecode(HttpContext.Current.Request["TYPE"]);
            if (string.IsNullOrEmpty(temp)) { temp = "xzxm"; }
            string sqlStr = string.Empty;
            if (!string.IsNullOrEmpty(txt_name.Text.Trim()))
            {
                if (temp == "xzxm" || temp == "xzxmzj" || temp == "xzxmx" || temp == "xzxc" || temp == "sfxx" || temp == "jftz" || temp == "xfzdry" || temp == "房屋征收程序申请" || temp == "房屋征收立项申请"
                    || temp == "房屋征收冻结申请" || temp == "房屋征收补偿决定审批" || temp == "房屋征收决定申请")
                {
                    sqlStr += " and t.项目名称 like '%" + txt_name.Text.Trim() + "%'";

                }

                if (temp == "房屋征收补偿实施方案申请")
                {
                   // sqlStr += " and 产权人 like '%" + txt_name.Text.Trim() + "%'";
                }
                if (temp == "khyh")
                {
                    sqlStr += " and z.单位名称 like '%" + txt_name.Text.Trim() + "%'";
                }
                if (temp == "房屋征收补偿资金监管")
                {
                    sqlStr += " and t.征收实施单位 like '%" + txt_name.Text.Trim() + "%'";
                }
            }
            if (temp != "房屋征收补偿实施方案申请" && temp != "khyh")
            {
                if (_EluZSDataProvider.IsQZSBM(userid) && !_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
                {
                    sqlStr += @" and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
Where  t.userid Like '%" + userid + "%') Like '%'||t.区属||'%' ";

                }
            }
            if (!string.IsNullOrEmpty(txtAddress.Text.Trim()))
            {
                if (temp == "xzxm" || temp == "xzxmzj" || temp == "xzxmx" || temp == "xzxc" || temp == "sfxx" || temp == "jftz" || temp == "xfzdry" || temp == "房屋征收程序申请" || temp == "房屋征收立项申请"
                        || temp == "房屋征收冻结申请" || temp == "房屋征收补偿决定审批" || temp == "房屋征收决定申请")
                {
                    sqlStr += " and t.项目地址 like '%" + txtAddress.Text.Trim() + "%'";
                }

            }

            if (temp == "sfxx")
            {
                if (_EluZSDataProvider.IsQZSBM(userid) && !_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
                {
                    sqlStr += @" and t.区属 in(select keyname from zs_params where name='区属'and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
        Where  t.userid Like '%" + userid + "%') Like '%'||keyname||'%')";
                }
            }
            return sqlStr;
        }
        protected void CustomGridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            TableCell cell = new TableCell();

            if (e.Row.RowType == DataControlRowType.Header)
            {
                TableHeaderCell cell1 = new TableHeaderCell();
                cell1.Text = "选择";
                e.Row.Cells.AddAt(0, cell1);
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rows = e.Row.DataItem as DataRowView;
                HtmlInputRadioButton rb = new HtmlInputRadioButton();

                if (rows != null)
                {
                    e.Row.Attributes.Add("id", rows["ID"].ToString());

                    rb.Attributes.Add("onclick", "setRdValue(this);");
                    rb.Attributes.Add("id", rows["ID"].ToString());
                }
                else
                {
                    e.Row.Attributes.Add("id", "");
                    rb.ID = "";
                    rb.Attributes.Add("onclick", "setRdValue(this);");
                }
                cell.Controls.Add(rb);
                e.Row.Cells.AddAt(0, cell);
            }

        }
        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            BinderData();
        }
        protected void bt_query_Click(object sender, EventArgs e)
        {
            BinderData();
        }
    }
}


